package com.umeox.watch.motioncapture.ui

import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import androidx.core.graphics.toColorInt
import com.umeox.watch.motioncapture.R
import com.umeox.watch.motioncapture.model.GsensorData
import com.umeox.watch.motioncapture.ui.UnitSelectActivity.Companion.INTENT_KEY_UNIT
import com.umeox.watch.motioncapture.upload.UploadManager
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale

class PrayerActionsActivity : AppCompatActivity(), SensorEventListener {

    private var gatherStatus = false
    private var sensorManager: SensorManager? = null
    private var sensor: Sensor? = null
    private lateinit var bt: Button
    private val uploadManager = UploadManager()
    private var unit = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        unit = intent?.getIntExtra(INTENT_KEY_UNIT, 0) ?: 0
        setContentView(R.layout.activity_prayer_actions)
        bt = findViewById(R.id.bt)
        updateUIByStatus()
        bt.setOnClickListener {
            if (gatherStatus) {
                stopGather()
            } else {
                startGather()
            }
            updateUIByStatus()
        }
    }

    private fun updateUIByStatus() {
        if (gatherStatus) {
            bt.text = getString(R.string.stop)
            bt.setBackgroundColor("#F40E0A".toColorInt())
        } else {
            bt.text = getString(R.string.start)
            bt.setBackgroundColor("#32E038".toColorInt())
        }
    }

    private fun startGather() {
        if (sensorManager == null) {
            sensorManager = getSystemService(SENSOR_SERVICE) as SensorManager
        }

        if (sensor == null) {
            sensor = sensorManager?.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
        }

        sensor?.let {
            uploadManager.init(this, "PrayerActions_${unit}units_${getCurrentDateTimeStr()}.csv")
            gatherStatus = true
            sensorManager?.registerListener(
                this@PrayerActionsActivity,
                it,
                SensorManager.SENSOR_DELAY_GAME
            )
        }
    }

    private fun getCurrentDateTimeStr(
        simpleDateFormat: SimpleDateFormat = SimpleDateFormat(
            "yyyyMMddHHmmss",
            Locale.CHINA
        )
    ): String {
        return simpleDateFormat.format(Date())
    }

    private fun stopGather() {
        gatherStatus = false
        sensorManager?.unregisterListener(this)
        sensorManager = null
        sensor = null
        uploadManager.stopGather()
    }

    override fun onDestroy() {
        super.onDestroy()
        stopGather()
    }


    override fun onSensorChanged(event: SensorEvent?) {
        val x = event?.values?.get(0)
        val y = event?.values?.get(1)
        val z = event?.values?.get(2)
        if (x != null && y != null && z != null) {
            val data = GsensorData(
                System.currentTimeMillis(),
                x,
                y,
                z
            )
            uploadManager.gatherData(data)
        }
    }

    override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {

    }

}